<?php
class ColleagueModel extends Model{
	protected $tableName = "user_colleague";

	
	function getGroup( $cid ){
		$list = $this->query(" SELECT * FROM ".C('DB_PREFIX')."user_colleague_relation WHERE cid=".$cid);
		return $list;
	}
	
	function doeditGroup( $cid , $gids ){
		$delhandle = $this->execute("DELETE FROM ".C('DB_PREFIX')."user_colleague_relation WHERE cid=".$cid);
		foreach($gids as $key=>$value){
			if($value) $arr[] = "($cid,$value)";
		}
		$insert = $this->execute("INSERT INTO ".C('DB_PREFIX')."user_colleague_relation (cid,gid) VALUES ".implode(',',$arr));
	}
	
	//获取用户同事分组列表
	function getUserGroup( $uid ){
		$group_list = M('user_colleague_group')->where('uid='.$uid)->field('id,name')->order('id ASC')->findall();
		foreach( $group_list as $value){
			$grouplist[ $value['id'] ] = $value['name'];
		}
		unset( $group_list );
		return $grouplist;
	}
	
	//添加一个同事至联系人
	function doaddOne($uid,$fid,$gid,$remark,$attention=false){
		if($uid && $fid){
			$savedata['uid'] = intval($uid);
			$savedata['fid'] = intval($fid);
			
			if( $info = $this->where($savedata)->field('cid')->find() ){
				$cid = $info['cid'];
			}else{
				$cid = $this->add($savedata);
				if($attention){  //关注
					$sFollow = x('Follow');$sFeed = x('Feed');$sNotify = x('Notify');
						if($sFollow->addFollowData($fid,'user',$uid)){
						$title['followuid']   = $fid;
						$title['followuname'] = getUserName($fid);
						$sFeed->put('space_follow',$title,'',$uid);
						$sNotify->put('space_follow',$fid,'','',$uid);
					}
				}		
			}
			
			$this->doaddToGroup( $cid,$gid );

			return '1';    //添加成功
		}else{
			return '-2';       //已经是同事
		}
	}
	
	//添加同事到分组
	private function doaddToGroup( $cid , $gid  ){
		if($gid){
			return $this->execute("INSERT INTO ".C('DB_PREFIX')."user_colleague_relation (cid,gid) VALUES ($cid,$gid)");
		}
	}
	
	//添加多个至我的联系人
	function doaddAll($uid,$data){
		$n = 1;
		foreach ($data['fid'] as $key=>$value){
			if($this->doaddOne($uid,$value,$data['gid'][$key],$data['remark'][$key])=='1'){
				$returns[] = $data['fid'][$key];
				if( $data['attention'][$key] ){
					$attention[]     = $value;
					if( $n <= 5){
						$attentionname[] = "<a href='".U('space/View/index',array('uid'=>$value))."' target='_blank'>".getUserName($value)."</a>";
					}
					$n ++;
				}
			}
		}
		
		$suffix = (count($attention)>5)?'...':'';
		
		if($attention){  //关注
			$sFollow = x('Follow');$sFeed = x('Feed');$sNotify = x('Notify');
				if($sFollow->addFollowData($attention,'user',$uid)){
				$title['followuid']   = $fid;
				$title['followuname'] = implode( '、' , $attentionname ).$suffix;
				$title['countnum']    = count( $attention );
				$sFeed->put('space_follow_much',$title,'',$uid);
				$sNotify->put('space_follow',implode(',',$attention),'','',$uid);
			}
		}			
		return $returns;
	}

	//检验用户是否激活  两类数组  haveInit已激活会员  unhaveInit未激活
	function __checkParseData($data){
//		$data = $this->__parseData($content);
//		$data = $data['format'];
		foreach ($data as $key=>$val){
			$info =  M('User')->where("uname='".$val."'")->field('uid,uname,email,fullname')->find();
			if($info['uid']){		
				if($info['uid']!=$_SESSION['mid']){
					 $cinfo = M('user_colleague')->field('gid')->where("uid=".$_SESSION['mid']." AND fid=".$info['uid'])->count();
					 if($cinfo) $info['isColleague'] = $cinfo;
					 $info['key'] = $key;
					 $arr['haveInit'][] = $info;
				}
			}else{
				if($val){
					$arr['unhaveInit'][]= array(
						'key'     => $key,
						'uname'   => $val,
						'notemail' => $result.'@notesmail.huawei.com"',
					);
				}
			}
		}
		return $arr;
	}
		
	//解析输入的数据
	function __parseData($content){
		$content = str_replace(array("\r\n", "\n", "\r"),',',$content);  //转换成换行
		$content = explode(",",$content);    //以逗号分割
		foreach ($content as $key=>$value){
			$val = trim($value);
			$result = $this->__checkparse($val);
			if($result){
				$data['format'][$key]['uname']     = $result;
				$data['format'][$key]['notesmail'] = $value;
			}
		}
		return $data;
	}
	
	function __checkparse($data){
		if(preg_match('/^(?!public)(.*\/huawei(01)?)/',$data)){
			$strbefore = substr($data,0,strpos($data,'/',0));
			$val = explode(" ",$strbefore);			
			return strtolower(substr($val[0],0,1)).end($val);
		}
	}

	//执行邀请操作
	function doinvite($uid,$invitemessage,$data){
		$invitecode = x('Invite')->getCode($uid,'colleague',$data['uname'],$data);
		$service = x("Mail");
		$senderInfo = array(
		   'name' => getUserName($uid),
		   'email'=> getUserEmail($uid),
		);		

		$title     = $senderInfo['name'].'邀请您加入华为研发知识共享平台';
		$path = SITE_URL;
		$body = <<<EOT
<div style="color:#000;font-size:12px;">
Dear:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{$title}:
<div style="margin:30px;">
"{$invitemessage}"
</div>
<div style="margin-left: 30px;">
		点击进入：<a href="{$invitecode}">{$invitecode}</a>
</div>
=============================================================================<br />
&nbsp;&nbsp;&nbsp;&nbsp;“热爱分享，畅想成长”<br />
&nbsp;&nbsp;&nbsp;&nbsp;知识共享平台：<a href="{$path}">{$path}</a><br />
=============================================================================
</div>
EOT;
		$service->send_email($data['uname']."@notesmail.huawei.com",$title,$body,$senderInfo);

	}
	

}
?>